<?php
/*  	maintenance_id
		equipment_id
		company_id
		schedule_id
		date_scheduled
		date_complete
		issue
		resolution
		item_ids
		manhours
		downtime
*/
function reequipment_user_maintenance()
{
	// reminder, as you add phases, they need to be added to the allowed input.
    if (!xarVarFetch('phase', 'enum:ajax_create:delete:form_eqview:show_maintenance:ajax_complete:ajax_update:', $phase, 'form', XARVAR_NOT_REQUIRED, XARVAR_PREP_FOR_DISPLAY)) return;
    if (!xarVarFetch('maintenance_id', 'int:1:', $maintenance_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('equipment_id', 'int:1:', $equipment_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('cycles', 'int:1:', $cycles, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('frequency_id', 'int:1:', $frequency_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('issue', 'str:1:', $issue, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('resolution', 'str:1:', $resolution, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('manhours', 'float', $manhours, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('downtime', 'float', $downtime, '', XARVAR_NOT_REQUIRED)) return;
    xarTplSetPageTitle(xarML('Maintenance Actions'));
    if (!xarSecurityCheck('EditEquipment', 1)) return;
    $data = array();

    // Get company_id
    if (!xarVarFetch('company_id', 'int:1:', $company_id, '', XARVAR_NOT_REQUIRED)) return;
	if (!xarModAPIFunc('reequipment', 'user', 'getcompanyid', array('company_id_from_url' => $company_id))) return;
	$company_id = xarSessionGetVar('company_id');

    switch(strtolower($phase)) {

        case 'form_eqview':

            $data['equipment'] = xarModAPIFunc('reequipment', 'user', 'getequipment', array('equipment_id' => $equipment_id, 'company_id' => $company_id));
            // If this is not assigned to this company exception out.
            if ($data['equipment'] == NULL){
            	$message = true;
            } else {
            	$message = false;
            }
            $data['frequency'] = xarModAPIFunc('reequipment', 'user', 'getfrequency');
            $template = xarTplModule('reequipment', 'ajax', 'eq_maintenance_add', array('equipment_id' => $equipment_id, 'message' => $message, 'frequency' => $data['frequency']));
			return $template;
        break;

        case 'ajax_create':
	       		$postinfo = xarModAPIFunc('reequipment', 'admin', 'createmaintenance', array('issue' => $issue, 'resolution' => $resolution, 'manhours' => $manhours, 'equipment_id' => $equipment_id, 'company_id' => $company_id, 'downtime' => $downtime, 'frequency_id' => $frequency_id));
                return true;
        break;

        case 'show_maintenance':
        		// PM
                $data['frequency'] = xarModAPIFunc('reequipment', 'user', 'getfrequency');
	       		$data['pm'] = xarModAPIFunc('reequipment', 'user', 'getmaintenance', array('company_id' => $company_id, 'equipment_id' => $equipment_id, 'date_complete' => true, 'scheduled' => true));
				// Unscheduled
	       		$data['unscheduled'] = xarModAPIFunc('reequipment', 'user', 'getmaintenance', array('company_id' => $company_id, 'equipment_id' => $equipment_id, 'date_complete' => true, 'unscheduled' => true));
	       		// return var_export($data['maintenance']);
                $template = xarTplModule('reequipment', 'ajax', 'show_maintenance', array('pm' => $data['pm'], 'equipment_id' => $equipment_id, 'unscheduled' => $data['unscheduled'], 'frequency' => $data['frequency']));
				return $template;
        break;

        case 'delete':
        		// return $equipment_id;
                $postinfo = xarModAPIFunc('reequipment', 'admin', 'deletemaintenance', array('maintenance_id' => $maintenance_id, 'company_id' => $company_id));
                xarResponseRedirect(xarModURL('reequipment', 'user', 'maintenance', array('company_id' => $company_id)));
        break;

		case 'ajax_complete':
			if (empty($resolution)){
				$resolution = xarML('Completed');
			}
			// Update the cycle count for the machine.
			$postinfo = xarModAPIFunc('reequipment', 'admin', 'updateequipment', array('equipment_id' => $equipment_id, 'cycles' => $cycles));
			// Update the rest of the information.
			$postinfo = xarModAPIFunc('reequipment', 'admin', 'updatemaintenance', array('maintenance_id' => $maintenance_id, 'issue' => $issue, 'resolution' => $resolution, 'manhours' => $manhours, 'equipment_id' => $equipment_id, 'company_id' => $company_id, 'downtime' => $downtime, 'frequency_id' => $frequency_id));
			return true;
        break;

		case 'ajax_update':
			$postinfo = xarModAPIFunc('reequipment', 'admin', 'updateequipment', array('equipment_id' => $equipment_id, 'cycles' => $cycles));
			// Update the rest of the information.
			$postinfo = xarModAPIFunc('reequipment', 'admin', 'updatemaintenance', array('maintenance_id' => $maintenance_id, 'issue' => $issue, 'manhours' => $manhours, 'equipment_id' => $equipment_id, 'company_id' => $company_id, 'downtime' => $downtime, 'frequency_id' => $frequency_id));
			return true;
        break;
    }
    // Return the output
    return $data;
}

?>